package com.itany.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.nmms.entity.Dept;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.DeptService;
import com.itany.nmms.service.StaffService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 石小俊
 * @date 2024年04月17日 18:50
 */
@RequestMapping("/staff")
public class StaffController {

    private StaffService staffService = (StaffService) ObjectFactory.getObject("staffService");
    private DeptService deptService = (DeptService) ObjectFactory.getObject("deptService");

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response) {
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        String staffName = request.getParameter("staffName");
        String loginName = request.getParameter("loginName");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String deptId = request.getParameter("deptId");
        String role = request.getParameter("role");
        String isValid = request.getParameter("isValid");
        request.setAttribute("staffName", staffName);
        request.setAttribute("loginName", loginName);
        request.setAttribute("phone", phone);
        request.setAttribute("email", email);
        request.setAttribute("deptId", deptId);
        request.setAttribute("role", role);
        request.setAttribute("isValid", isValid);

        try {
            List<Dept> depts = deptService.findAll();
            request.setAttribute("depts", depts);

            PageInfo<Staff> staffPage = staffService.findPage(pageNo, pageSize, staffName, loginName, phone, email, deptId, role, isValid);
            request.setAttribute("staffPage", staffPage);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return "backend/staffManager";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String loginName = request.getParameter("loginName");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        String code = request.getParameter("code");
        String image = (String) request.getSession().getAttribute("code");

        try {
            Staff staff = staffService.login(loginName, password, role, code, image);
            // 如果登录成功,将当前员工信息保存到session中
            request.getSession().setAttribute("staff", staff);

            // 当前方法的返回值有三种格式
            // 1.redirect:url   表示使用重定向跳转到指定的url地址
            return "redirect:/showMain.do";
        } catch (RequestParameterErrorException e) {
            // 2.url    表示使用转发跳转到指定的url地址
            request.setAttribute("loginMsg", e.getMessage());
            return "/showLogin.do";
        } catch (CodeErrorException e) {
            // 3.视图名    表示使用转发跳转到指定的页面
            // 此时的页面需要经过DispatcherServlet处理
            // 即最终访问页面地址为:前缀+视图名+后缀
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        } catch (StaffNotExistException e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        } catch (ServiceException e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return "backend/login";
    }
}
